﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace FACUI.BasicClass
{

    public class GetZnfxMapItem
    {
        private Dictionary<string, string> kglAdList;
        private IniFile linkFile;
        private string znfxZctFielPath;

        public GetZnfxMapItem(string znfxzctFielPath, Dictionary<string, string> kgladList)
        {
            this.znfxZctFielPath = "";
            this.linkFile = null;
            this.znfxZctFielPath = znfxzctFielPath;
            this.kglAdList = kgladList;
            this.InitZnfxZct();
        }

        public GetZnfxMapItem(IniFile inifile, Dictionary<string, string> kgladList)
        {
            this.znfxZctFielPath = "";
            this.linkFile = null;
            this.linkFile = inifile;
            this.kglAdList = kgladList;
        }

        public IniFile GetlinkFile()
        {
            return this.linkFile;
        }
         
        public Dictionary<string, MapLogicKglItem> GetZnfxLogicList()
        {
            Dictionary<string, MapLogicKglItem> dictionary = new Dictionary<string, MapLogicKglItem>();
            if (this.linkFile == null)
            {
                MessageBox.Show("文件不存在，无法生成!", "生成失败");
                return null;
            }
            if (!this.linkFile.ContainSectionName("逻辑开关量"))
            {
                MessageBox.Show("没有找到逻辑开关量字段，无法生成!", "生成失败");
                return null;
            }
            MapLogicKglItem item = null;
            string[] strArray = null;
            string[] strArray2 = null;
            int num = 0;
            foreach (string str in this.linkFile.GetSectionItemName("逻辑开关量"))
            {
                strArray2 = this.linkFile.GetString("逻辑开关量", str).Split(new char[] { ',', (char)0xff0c });
                if (strArray2.Length >= 3)
                {
                    item = new MapLogicKglItem();
                    if (!string.IsNullOrEmpty(strArray2[0]))
                    {
                        item.KglName = strArray2[0].Trim();
                        item.Relation = Enum.GetName(typeof(GetMapItem.logicRelation), Util.ToInt(strArray2[1], 0));
                        item.DelFlag = 1;
                        num = Util.ToInt(strArray2[2], 0);
                        if ((num == 2) && (strArray2.Length >= 5))
                        {
                            strArray = strArray2[3].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl1 = strArray[0].Trim();
                            item.Not1 = strArray[1] == "1";
                            strArray = strArray2[4].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl2 = strArray[0].Trim();
                            item.Not2 = strArray[1] == "1";
                        }
                        else if ((num == 3) && (strArray2.Length >= 6))
                        {
                            strArray = strArray2[3].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl1 = strArray[0].Trim();
                            item.Not1 = strArray[1] == "1";
                            strArray = strArray2[4].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl2 = strArray[0].Trim();
                            item.Not2 = strArray[1] == "1";
                            strArray = strArray2[5].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl3 = strArray[0].Trim();
                            item.Not3 = strArray[1] == "1";
                        }
                        if (!dictionary.ContainsKey(item.KglName))
                        {
                            dictionary.Add(item.KglName, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        private void InitZnfxZct()
        {
            string str = this.znfxZctFielPath.Substring(this.znfxZctFielPath.LastIndexOf(@"\") + 1, (this.znfxZctFielPath.Length - this.znfxZctFielPath.LastIndexOf(@"\")) - 1);
            IniFile file = null;
            file = new IniFile(this.znfxZctFielPath);
            file.Load();
            string iNIFile = Application.StartupPath + @"\tmpfile\znfx_" + str;
            this.linkFile = new IniFile(iNIFile);
            string str3 = (this.znfxZctFielPath == "") ? file.FileName : this.znfxZctFielPath;
            string arguments = "DevLink \"" + str3 + "\" \"" + iNIFile + "\"";
            Process process = Process.Start(Application.StartupPath + @"\tmpfile\ProtectZctCAD.exe", arguments);
            if (process.WaitForExit(0x30d40))
            {
                this.linkFile.Load();
            }
            else
            {
                MessageBox.Show("等待超时，请重新生成！", "提示");
                process.Close();
            }
        }
    }
}

